/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dacnpm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author SONY
 */
public class Form_DangNhap extends javax.swing.JFrame {

    Statement stt;
    Connection conn;
    ResultSet rs;
    public static String  name=null;
    public Form_DangNhap() throws Exception {
        initComponents();
         LoadDB();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        bt1_dangnhap = new javax.swing.JButton();
        tf1_dangnhap = new javax.swing.JTextField();
        bt1_help = new javax.swing.JButton();
        tf1_password = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setToolTipText("");
        jPanel1.setPreferredSize(new java.awt.Dimension(800, 600));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        bt1_dangnhap.setText("Dang Nhap");
        bt1_dangnhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt1_dangnhapActionPerformed(evt);
            }
        });
        jPanel1.add(bt1_dangnhap, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 460, 140, 50));

        tf1_dangnhap.setName(""); // NOI18N
        tf1_dangnhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf1_dangnhapActionPerformed(evt);
            }
        });
        jPanel1.add(tf1_dangnhap, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 340, 260, 50));

        bt1_help.setText("Bạn đang cần gì?");
        bt1_help.setBorder(null);
        bt1_help.setBorderPainted(false);
        bt1_help.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        bt1_help.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        bt1_help.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt1_helpActionPerformed(evt);
            }
        });
        jPanel1.add(bt1_help, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 525, 140, 20));
        jPanel1.add(tf1_password, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 400, 260, 50));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Username");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 340, 70, 50));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Password");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 400, 70, 50));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dacnpm/slide1.jpg"))); // NOI18N
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, -40, 790, 670));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
public  void LoadDB() throws Exception{
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String url ="Jdbc:odbc:user";
		conn = DriverManager.getConnection(url);
		stt=conn.createStatement();		
               
	}
public int checkuser() throws SQLException{
       String sql="Select * from User";
       rs = stt.executeQuery(sql);
		while (rs.next()){
                    String namedb=rs.getString("name");
                    String passdb=rs.getString("password");
                    int accountdb = Integer.parseInt(rs.getString("account"));
                    if((tf1_dangnhap.getText().equals(namedb)) &&(tf1_password.getText().equals(passdb))){
                       if(accountdb==0){
                           name=tf1_dangnhap.getText();
                           return 1;
                           
                       }                           
                       else{
                           return 2;
                       }                          
                    }
                }
		return 0;
}
    private void tf1_dangnhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf1_dangnhapActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf1_dangnhapActionPerformed

    private void bt1_dangnhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt1_dangnhapActionPerformed
        try {              
            if(checkuser()==2){               
                this.setVisible(false) ;
                Form_QuanTri t2= new Form_QuanTri();
                t2.setVisible(true);                    
           } 
            else if(checkuser()==1){
                  this.setVisible(false) ;
                new Form_Thi().setVisible(true);
            }else{
                JOptionPane.showMessageDialog(null, "Tài khoản hoặc mật khẩu không đúng!");
                
            }   
        } catch (SQLException ex) {
        
        } catch (Exception ex) {
            Logger.getLogger(Form_DangNhap.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bt1_dangnhapActionPerformed

    private void bt1_helpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt1_helpActionPerformed
        JOptionPane.showMessageDialog(rootPane, "Đây là chương trình thi trắc nghiệm trên máy đơn."
                + "\nBạn cần phải đăng nhập để thực hiện hành động thi.");
    }//GEN-LAST:event_bt1_helpActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Form_DangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Form_DangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Form_DangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Form_DangNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Form_DangNhap().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(Form_DangNhap.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt1_dangnhap;
    private javax.swing.JButton bt1_help;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField tf1_dangnhap;
    private javax.swing.JPasswordField tf1_password;
    // End of variables declaration//GEN-END:variables
}
